package com.cloudcc.mobile.view.web;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrClassicFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrDefaultHandler;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrHandler;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.view.activity.BindActivity;

/**
 * 侧拉网页加载页面
 * 
 * @author Administrator
 * 
 */
@SuppressLint("SetJavaScriptEnabled")
public class NewWebViewActivtiy extends BaseWebActivity {
	private String mEns = RunTimeManager.getInstance().getlanguage();
	private PtrClassicFrameLayout mptrframe;

	@Override
	protected void initUI() {
		super.initUI();
		mWebView = (WebView) findViewById(R.id.new_webview);
		mptrframe = (PtrClassicFrameLayout) findViewById(R.id.new_webview_loadmore);
		mptrframe.setLastUpdateTimeRelateObject(this);
		mptrframe.setPtrHandler(new PtrHandler() {
			@Override
			public boolean checkCanDoRefresh(PtrFrameLayout frame,
					View content, View header) {
				return PtrDefaultHandler.checkContentCanBePulledDown(frame,
						mWebView, header);
			}

			@Override
			public void onRefreshBegin(PtrFrameLayout frame) {
				mWebView.loadUrl(mUrl);
			}
		});
		mptrframe.setLoadingMinTime(1000);
		mptrframe.setDurationToClose(200);
		mptrframe.setDurationToCloseHeader(1000);
		mptrframe.setResistance(1.7f);
		mptrframe.setRatioOfHeaderHeightToRefresh(1.2f);
		mptrframe.setPullToRefresh(false);
		mptrframe.setKeepHeaderWhenRefresh(true);
		mptrframe.postDelayed(new Runnable() {
			@Override
			public void run() {
				mptrframe.autoRefresh();
			}
		}, 150);
	}

	@Override
	protected void initSetting() {
		super.initSetting();

		mWebView.requestFocus();
		mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);// 防止滑动的时候闪屏
		mUrl = AppContext.urlString;

		WebSettings webstting = mWebView.getSettings();
		webstting.setJavaScriptEnabled(true);
		webstting.setAllowFileAccess(true);
		webstting.setSupportZoom(true);
		webstting.setCacheMode(WebSettings.LOAD_DEFAULT);// 是否缓存
		webstting.setBlockNetworkImage(false);
		webstting.setJavaScriptCanOpenWindowsAutomatically(true);

	}

	@Override
	public int getLayoutId() {
		return R.layout.newwebview;
	}

	@Override
	protected void initClient() {

		super.initClient();
		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			// 加载结束
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				mUrl = url;
				mptrframe.refreshComplete();
				stopProgress();
			}

			@Override
			// 点击超链接的时候重新在原来进程上加载URL
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				showProgress();
				if (UrlManager.isWapLogin(url)) {// 跳转去登录
					EventEngine.register(NewWebViewActivtiy.this);
					Intent intent = new Intent(mContext, BindActivity.class);
					startActivity(intent);
					return true;
				}
				if (UrlManager.isHome(url)) {
					NewWebViewActivtiy.this.finish();
					return true;
				} else {
					// 自身加载新链接,不做外部跳转
					view.loadUrl(url);
					return true;
				}
			}

			@Override
			// 显示错误页面
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				AppContext.isError = true;
			
//if ("en".equals(mEns)) {
//	view.loadUrl("file:///android_asset/html/en_error.html");



//	}else{
		view.loadUrl("file:///android_asset/html/error.html");



//}
			}
		});
	}

}
